//
//  BIDBillsManager.m
//  随手记01
//
//  Created by zd2011 on 13-5-23.
//  Copyright (c) 2013年 邵瑞. All rights reserved.
//

#import "BIDBillsManager.h"
#import "BIDDatabaseManager.h"
#define kTableName @"MessageInfoTable"//保存用户输入数据的表 
#define kDatabaseHadYears @"hadYears"
#define kDatabaseHadMonths @"hadMonths"
@interface BIDBillsManager()
{
    FMDatabase *aDatabase;
}
@end
@implementation BIDBillsManager
+(BIDBillsManager *)shareBillsManager
{
    static BIDBillsManager *billsManager=nil;
    static dispatch_once_t predicate;//一个命令
    dispatch_once(&predicate, ^
                  {//整个生命期子被调用一次
                      
                      billsManager = [[self alloc] init];//
                  });
    return billsManager;

}
-(id)init
{
    aDatabase=[BIDDatabaseManager SharedOperator].fmdbDataBase;
    return self;  
    
}
#pragma mark--aBillManager
-(BOOL)insertAPayout:(BIDABill *)aPayout
{
    BOOL judge=NO;
   
    NSString *inset=[NSString stringWithFormat:@"insert into %@ (amount,type_ID,subType_ID,time,other,whichWay) values ('%f','%d','%d','%@','%@','%d')",kTableName,aPayout.Camount,aPayout.type_ID,aPayout.subType_ID,aPayout.Ctime,aPayout.Cdescription,aPayout.whichWay];
    judge = [aDatabase executeUpdate:inset];
    return judge;
}
 //messageInfo_ID integer primary key autoincrement,amount double,type_ID integer,subType_ID integer,date text,other text,
-(BOOL)updataThePayout:(BIDABill *)NewPayout andOldPayout:(BIDABill *)oldPayout
{
    BOOL judge=NO;
    if (NewPayout.type_ID!=oldPayout.type_ID)
    {
        NSString *update=[NSString stringWithFormat:@"update %@ set type_ID=%d where messageInfo_ID=%d",kTableName,NewPayout.type_ID,oldPayout.payoutId];
        judge=[aDatabase executeUpdate:update];

    }
    if (NewPayout.subType_ID!=oldPayout.subType_ID) {
        NSString *update1=[NSString stringWithFormat:@"update %@ set subType_ID=%d  where messageInfo_ID=%d",kTableName,NewPayout.subType_ID,oldPayout.payoutId];
        judge=[aDatabase executeUpdate:update1];
    }
    if (NewPayout.Camount!=oldPayout.Camount) {
        NSString *update2=[NSString stringWithFormat:@"update %@ set amount=%f  where messageInfo_ID=%d",kTableName,NewPayout.Camount,oldPayout.payoutId];
        judge=[aDatabase executeUpdate:update2];
    }
    if (![NewPayout.Ctime isEqualToString:oldPayout.Ctime]) {
        NSString *update3=[NSString stringWithFormat:@"update %@ set time='%@'  where messageInfo_ID=%d",kTableName,NewPayout.Ctime,oldPayout.payoutId];
        judge=[aDatabase executeUpdate:update3];
    }
    if (![NewPayout.Cdescription isEqualToString:oldPayout.Cdescription]) {
        NSString *update4=[NSString stringWithFormat:@"update %@ set other='%@'  where messageInfo_ID=%d",kTableName,NewPayout.Cdescription,oldPayout.payoutId];
        judge=[aDatabase executeUpdate:update4];
    }
    if (NewPayout.whichWay!=oldPayout.whichWay) {
      NSString *update5=[NSString stringWithFormat:@"update %@ set whichWay=%d  where messageInfo_ID=%d",kTableName,NewPayout.whichWay,oldPayout.payoutId];
   judge=[aDatabase executeUpdate:update5];
}
    return judge;
}
-(BOOL)deleteAPayout:(BIDABill *)payout
{
    BOOL judge=NO;
    NSString *delete=[NSString stringWithFormat:@"delete  from %@ where messageInfo_ID=%d",kTableName,payout.payoutId];
    judge=[aDatabase executeUpdate:delete];
    return judge;
}

@end